package com.system.healthproject.controller;

import com.system.healthproject.bean.AliResponseEntity;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.utils.RandomUtils;
import com.system.healthproject.utils.RedisUtil;
import com.system.healthproject.utils.SendSmsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@Api(value = "短信发送",tags = "公共-短信发送API")
@RestController
@RequestMapping("/sms")
@Validated
public class SmsController {

    @Autowired
    RedisUtil redisUtil;


    @ApiOperation(value = "短信发送接口",httpMethod = "post")
    @PostMapping(value = "/sendSms",produces = "application/json;charset=utf-8")
    public ApiResult sendSms(@NotBlank(message = "手机号不能为空") @RequestParam(value = "phone" ,required = false) String phone){

        String code = RandomUtils.produceNumber();
        String phoneKey = phone + "code";

        redisUtil.set(phoneKey,code,60);
        AliResponseEntity aliResponseEntity = SendSmsUtils.sendSms(phone, code);
        System.out.println(aliResponseEntity);

        if(aliResponseEntity.getCode().equals("OK")){
            return ApiResult.success("短信发送成功");
        }else {
            return ApiResult.failure("短信发送失败");
        }
    }
}
